/**
  ******************************************************************************
  * File Name          : ctrl_fan.c
  * Description        : Code for data process applications
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "tskcfg.h"
#include "bsp.h"
#include "collector.h"
#include "spiflash.h"
#include "ctrl_common.h"

static const uint8_t vfd_sptp = 1;
static const uint8_t vfd_full_range = vfd_sptp * 10; /* 变频器满量程 0.1(度) * 10(级) */


void Ctrl_fan_nh3(uint8_t nh3)
{
  if(nh3 > Ctrl_ParaJT.fnhin3)
  {
    fn_act[0] = 1;
    Ctrl_StateFN.fnst[0] = 'A';
  }
}

void Ctrl_fan_mode_jt(int16_t temper)
{
  uint8_t ch, j;
	int32_t diff;
	int16_t fn_hitp;
	
	/* 始终扫描4个变频器, 通道未开的无动作 */
	for(ch=0, j=0; ch<CTR_VFD_CHANNEL_ALLNUM; ch++)
	{
		if(Ctrl_ParaTD.vftd[ch] != 0)
		{
			diff = ((temper - Ctrl_ParaJT.fnhitp - (vfd_full_range * j)) / vfd_sptp) + 1;
			if(diff <= 0)
			{
				vf_act[ch] = 0;
				Ctrl_StateVF.vfjs[ch] = 0;
				Ctrl_StateVF.vfst[ch] = 'j';
			}				
			else if(diff >= 10)
			{
				vf_act[ch] = 10;
				Ctrl_StateVF.vfjs[ch] = 10;
				Ctrl_StateVF.vfst[ch] = 'J';
			}
			else
			{
				vf_act[ch] = diff;
				Ctrl_StateVF.vfjs[ch] = vf_act[ch];
				Ctrl_StateVF.vfst[ch] = 'J';
			}

			j++;
		}
	} 
	
	fn_hitp = (j * vfd_full_range) + Ctrl_ParaJT.fnhitp;
	
  for(ch=0, j=0; ch<CTR_FAN_CHANNEL_ALLNUM; ch++)
  {
    if(Ctrl_ParaTD.fntd[ch] == 0)
    {
      fn_act[ch] = 0;
      Ctrl_StateFN.fnst[ch] = 'n';
      continue;
    }
    
		if(temper <= Ctrl_ParaJT.fnlotp)
    {
      fn_act[ch] = 0;
      Ctrl_StateFN.fnst[ch] = 'l';
			continue;
    }
		
    fn_act[ch] = 2;
		if(CtrFan_GetActuator(ch) != 0)
		{
			Ctrl_StateFN.fnst[ch] = 'R';
		}
		else
		{
			Ctrl_StateFN.fnst[ch] = 'r';
		}

    if(temper >= (fn_hitp + (j * Ctrl_ParaJT.fnsptp)))
    {
      fn_act[ch] = 1;
      Ctrl_StateFN.fnst[ch] = 'J';
    }
    
    if(temper < (fn_hitp + (j * Ctrl_ParaJT.fnsptp) - Ctrl_ParaJT.fnrttp))
    {
      fn_act[ch] = 0;
      Ctrl_StateFN.fnst[ch] = 'j';
    }
    
    j++;
  }
}

void Ctrl_fan_mode_dl(int16_t temper, uint32_t ticker, const volatile uint32_t *vf_ticker)
{
  uint8_t ch;
  int32_t diff;
	
	for(ch=0; ch<CTR_VFD_CHANNEL_ALLNUM; ch++)
	{
		if(Ctrl_ParaTD.vftd[ch] == 0)
		{
			continue;
		}
		
		if(Ctrl_ParaDL1.vftp[ch] == 0)  /* 独立时控 */
		{
			if(Ctrl_ParaSK.vfen != 0)
			{
				if(Ctrl_ParaSK.vftf[ch] != 0)
				{
					Ctr_TcTickers.fan_tc_recont = 1;
					
					/* 同步风机的时控 */
					if(ticker <= Ctrl_ParaSK.fnkq)
					{
						vf_act[ch] = Ctrl_ParaSK.vfjs[ch];
						Ctrl_StateVF.vfst[ch] = 'S';
						Ctrl_StateVF.vfjs[ch] = vf_act[ch];
					}
					else if((ticker > Ctrl_ParaSK.fnkq) && (ticker <= (Ctrl_ParaSK.fnkq + Ctrl_ParaSK.fngb)))
					{
						vf_act[ch] = 0;
						Ctrl_StateVF.vfst[ch] = 's';
						Ctrl_StateVF.vfjs[ch] = 0;
					}
					else
					{
						vf_act[ch] = 0;
						Ctrl_StateVF.vfst[ch] = 's';
						Ctrl_StateVF.vfjs[ch] = 0;
					}
				}
				else
				{
					Ctr_TcTickers.vfd_tc_recont[ch] = 1;
					
					/* 变频器独立的时控 */
					if(vf_ticker[ch] <= Ctrl_ParaSK.vfkq[ch])
					{
						vf_act[ch] = Ctrl_ParaSK.vfjs[ch];
						Ctrl_StateVF.vfst[ch] = 'S';
						Ctrl_StateVF.vfjs[ch] = vf_act[ch];
					}
					else if((vf_ticker[ch] > Ctrl_ParaSK.vfkq[ch]) && (vf_ticker[ch] <= (Ctrl_ParaSK.vfkq[ch] + Ctrl_ParaSK.vfgb[ch])))
					{
						vf_act[ch] = 0;
						Ctrl_StateVF.vfst[ch] = 's';
						Ctrl_StateVF.vfjs[ch] = 0;
					}
					else
					{
						vf_act[ch] = 0;
						Ctrl_StateVF.vfst[ch] = 's';
						Ctrl_StateVF.vfjs[ch] = 0;
					}
				}
			}
			else
			{
				vf_act[ch] = 0;
				Ctrl_StateVF.vfst[ch] = 'd';
				Ctrl_StateVF.vfjs[ch] = 0;
			}
		}
		else
		{
			diff = ((temper - Ctrl_ParaDL1.vftp[ch]) / vfd_sptp) + 1;
			if(diff <= 0)
			{
				vf_act[ch] = 0;
				Ctrl_StateVF.vfjs[ch] = 0;
				Ctrl_StateVF.vfst[ch] = 'd';
			}
			else if(diff >= 10)
			{
				vf_act[ch] = 10;
				Ctrl_StateVF.vfjs[ch] = 10;
				Ctrl_StateVF.vfst[ch] = 'D';
			}
			else
			{
				vf_act[ch] = diff;
				Ctrl_StateVF.vfjs[ch] = vf_act[ch];
				Ctrl_StateVF.vfst[ch] = 'D';
			}
		}
	}
	
  for(ch=0; ch<CTR_FAN_CHANNEL_ALLNUM; ch++)
  {
    if(Ctrl_ParaDL1.fntp[ch] == 0)
    {
      if((Ctrl_ParaSK.fnen != 0) && (Ctrl_ParaSK.fntd[ch] != 0))
      {
				Ctr_TcTickers.fan_tc_recont = 1;
				
        if(ticker <= Ctrl_ParaSK.fnkq)
        {
          fn_act[ch] = 1;
          Ctrl_StateFN.fnst[ch] = 'S';
        }
        else if((ticker > Ctrl_ParaSK.fnkq) && (ticker <= (Ctrl_ParaSK.fnkq + Ctrl_ParaSK.fngb)))
        {
          fn_act[ch] = 0;
          Ctrl_StateFN.fnst[ch] = 's';
        }
				else
				{
					fn_act[ch] = 0;
          Ctrl_StateFN.fnst[ch] = 's';
				}
      }
      else
      {
        fn_act[ch] = 0;
        Ctrl_StateFN.fnst[ch] = 'd';
      }
    }
    else /* Ctrl_ParaDL.fntp[ch] != 0 */
    {
      fn_act[ch] = 2;
			if(CtrFan_GetActuator(ch) != 0)
			{
				Ctrl_StateFN.fnst[ch] = 'E';
			}
			else
			{
				Ctrl_StateFN.fnst[ch] = 'e';
			}
      
      if(temper >= Ctrl_ParaDL1.fntp[ch])
      {
        fn_act[ch] = 1;
        Ctrl_StateFN.fnst[ch] = 'D';
      }
      
      if(temper < (Ctrl_ParaDL1.fntp[ch] - Ctrl_ParaDL2.fnrttp))
      {
        fn_act[ch] = 0;
        Ctrl_StateFN.fnst[ch] = 'd';
      }
    }
  }
}

void Ctrl_fan_mode_sk(int16_t temper, uint32_t ticker, const volatile uint32_t *vf_ticker)
{
  uint8_t ch;
  
	/* 始终扫描4个变频器, 通道未开的无动作 */
	for(ch=0; ch<CTR_VFD_CHANNEL_ALLNUM; ch++)
	{
		if(Ctrl_ParaTD.vftd[ch] != 0)
		{
			if(Ctrl_ParaMD.vfmdsken == 0)
			{
				vf_act[ch] = 0;
				Ctrl_StateVF.vfst[ch] = 'q';
				Ctrl_StateVF.vfjs[ch] = 0;
				continue;
			}
			
			if(Ctrl_ParaSK.vfen == 0)
			{
				vf_act[ch] = 0;
				Ctrl_StateVF.vfjs[ch] = 0;
				Ctrl_StateVF.vfst[ch] = 'p';
				continue;
			}
			
			if(Ctrl_ParaSK.vftf[ch] != 0)
			{
				Ctr_TcTickers.fan_tc_recont = 1;
				
				/* 同步风机的时控 */
				if(ticker <= Ctrl_ParaSK.fnkq)
				{
					vf_act[ch] = Ctrl_ParaSK.vfjs[ch];
					Ctrl_StateVF.vfst[ch] = 'T';
					Ctrl_StateVF.vfjs[ch] = vf_act[ch];
				}
				else if((ticker > Ctrl_ParaSK.fnkq) && (ticker <= (Ctrl_ParaSK.fnkq + Ctrl_ParaSK.fngb)))
				{
					vf_act[ch] = 0;
					Ctrl_StateVF.vfst[ch] = 't';
					Ctrl_StateVF.vfjs[ch] = 0;
				}
				else
				{
					vf_act[ch] = 0;
					Ctrl_StateVF.vfst[ch] = 't';
					Ctrl_StateVF.vfjs[ch] = 0;
				}
			}
			else
			{
				Ctr_TcTickers.vfd_tc_recont[ch] = 1;
				
				/* 变频器独立的时控 */
				if(vf_ticker[ch] <= Ctrl_ParaSK.vfkq[ch])
				{
					vf_act[ch] = Ctrl_ParaSK.vfjs[ch];
					Ctrl_StateVF.vfst[ch] = 'T';
					Ctrl_StateVF.vfjs[ch] = vf_act[ch];
				}
				else if((vf_ticker[ch] > Ctrl_ParaSK.vfkq[ch]) && (vf_ticker[ch] <= (Ctrl_ParaSK.vfkq[ch] + Ctrl_ParaSK.vfgb[ch])))
				{
					vf_act[ch] = 0;
					Ctrl_StateVF.vfst[ch] = 't';
					Ctrl_StateVF.vfjs[ch] = 0;
				}
				else
				{
					vf_act[ch] = 0;
					Ctrl_StateVF.vfst[ch] = 't';
					Ctrl_StateVF.vfjs[ch] = 0;
				}
			}
		}
	} 
	
  for(ch=0; ch<CTR_FAN_CHANNEL_ALLNUM; ch++)
  {
		if(Ctrl_ParaMD.fnmdsken == 0)
		{
			fn_act[ch] = 0;
			Ctrl_StateFN.fnst[ch] = 'q';
			
			continue;
		}
		
    if((Ctrl_ParaSK.fnen != 0) && (Ctrl_ParaSK.fntd[ch] != 0))
    {
			Ctr_TcTickers.fan_tc_recont = 1;
			
      if(ticker <= Ctrl_ParaSK.fnkq)
      {
        fn_act[ch] = 1;
        Ctrl_StateFN.fnst[ch] = 'T';
      }
      else if((ticker > Ctrl_ParaSK.fnkq) && (ticker <= (Ctrl_ParaSK.fnkq + Ctrl_ParaSK.fngb)))
      {
        fn_act[ch] = 0;
        Ctrl_StateFN.fnst[ch] = 't';
      }
			else
			{
				fn_act[ch] = 0;
        Ctrl_StateFN.fnst[ch] = 't';
			}
    }
    else
    {
      fn_act[ch] = 0;
      Ctrl_StateFN.fnst[ch] = 'p';
    }
  }
}

void Ctrl_fan_mode_zn(int16_t temper, uint32_t ticker, const volatile uint32_t *vf_ticker)
{
  uint8_t ch, j;
	int32_t diff;
	int16_t fn_hitp;
	
	/* 始终扫描4个变频器, 通道未开的无动作 */
	for(ch=0, j=0; ch<CTR_VFD_CHANNEL_ALLNUM; ch++)
	{
		if(Ctrl_ParaTD.vftd[ch] == 0)
		{
			continue;
		}
		
		if(temper <= Ctrl_ParaJT.fnlotp)
		{
			vf_act[ch] = 0;
			Ctrl_StateVF.vfjs[ch] = 0;
			Ctrl_StateVF.vfst[ch] = 'w';
			continue;
		}
		
		diff = ((temper - Ctrl_ParaJT.fnhitp - (vfd_full_range * j)) / vfd_sptp) + 1;
		if(diff <= 0)
		{
			vf_act[ch] = 0;
			Ctrl_StateVF.vfjs[ch] = 0;
			Ctrl_StateVF.vfst[ch] = 'z';
		}				
		else if(diff >= 10)
		{
			vf_act[ch] = 10;
			Ctrl_StateVF.vfjs[ch] = 10;
			Ctrl_StateVF.vfst[ch] = 'Z';
		}
		else
		{
			vf_act[ch] = diff;
			Ctrl_StateVF.vfjs[ch] = vf_act[ch];
			Ctrl_StateVF.vfst[ch] = 'Z';
		}
		
		/* 时控条件符合了  覆盖上面的判决结果 */
		if((temper < Ctrl_ParaJT.fnhitp) && (Ctrl_ParaSK.vfen != 0) && (temper > Ctrl_ParaJT.fnlotp))
		{
			if(Ctrl_ParaSK.vftf[ch] != 0)
			{
				Ctr_TcTickers.fan_tc_recont = 1;
				
				/* 同步风机的时控 */
				if(ticker <= Ctrl_ParaSK.fnkq)
				{
					vf_act[ch] = Ctrl_ParaSK.vfjs[ch];
					Ctrl_StateVF.vfst[ch] = 'Y';
					Ctrl_StateVF.vfjs[ch] = vf_act[ch];
				}
				else if((ticker > Ctrl_ParaSK.fnkq) && (ticker <= (Ctrl_ParaSK.fnkq + Ctrl_ParaSK.fngb)))
				{
					vf_act[ch] = 0;
					Ctrl_StateVF.vfst[ch] = 'y';
					Ctrl_StateVF.vfjs[ch] = 0;
				}
				else
				{
					vf_act[ch] = 0;
					Ctrl_StateVF.vfst[ch] = 'y';
					Ctrl_StateVF.vfjs[ch] = 0;
				}
			}
			else
			{
				Ctr_TcTickers.vfd_tc_recont[ch] = 1;
				
				/* 变频器独立的时控 */
				if(vf_ticker[ch] <= Ctrl_ParaSK.vfkq[ch])
				{
					vf_act[ch] = Ctrl_ParaSK.vfjs[ch];
					Ctrl_StateVF.vfst[ch] = 'Y';
					Ctrl_StateVF.vfjs[ch] = vf_act[ch];
				}
				else if((vf_ticker[ch] > Ctrl_ParaSK.vfkq[ch]) && (vf_ticker[ch] <= (Ctrl_ParaSK.vfkq[ch] + Ctrl_ParaSK.vfgb[ch])))
				{
					vf_act[ch] = 0;
					Ctrl_StateVF.vfst[ch] = 'y';
					Ctrl_StateVF.vfjs[ch] = 0;
				}
				else
				{
					vf_act[ch] = 0;
					Ctrl_StateVF.vfst[ch] = 'y';
					Ctrl_StateVF.vfjs[ch] = 0;
				}
			}
		}
		
		j++;
	}
	
	fn_hitp = (j * vfd_full_range) + Ctrl_ParaJT.fnhitp;
	
  for(ch=0, j=0; ch<CTR_FAN_CHANNEL_ALLNUM; ch++)
  {
    if(Ctrl_ParaTD.fntd[ch] == 0)
    {
      fn_act[ch] = 0;
      Ctrl_StateFN.fnst[ch] = 'n';
      continue;
    }
    
    if(temper <= Ctrl_ParaJT.fnlotp)
    {
      fn_act[ch] = 0;
      Ctrl_StateFN.fnst[ch] = 'w';
      continue;
    }
    
    fn_act[ch] = 2;
		if(CtrFan_GetActuator(ch) != 0)
		{
			Ctrl_StateFN.fnst[ch] = 'X';
		}
		else
		{
			Ctrl_StateFN.fnst[ch] = 'x';
		}
		
    if(temper >= (fn_hitp + (j * Ctrl_ParaJT.fnsptp)))
    {
      fn_act[ch] = 1;
      Ctrl_StateFN.fnst[ch] = 'Z';
    }
    
    if(temper < (fn_hitp + (j * Ctrl_ParaJT.fnsptp) - Ctrl_ParaJT.fnrttp))
    {
      fn_act[ch] = 0;
      Ctrl_StateFN.fnst[ch] = 'z';
    }
    
		if((Ctrl_ParaSK.fnen != 0) && (Ctrl_ParaSK.fntd[ch] != 0) && (temper < Ctrl_ParaJT.fnhitp) && (temper > Ctrl_ParaJT.fnlotp))
    {
			Ctr_TcTickers.fan_tc_recont = 1;
			
      if(ticker <= Ctrl_ParaSK.fnkq)
      {
        fn_act[ch] = 1;
        Ctrl_StateFN.fnst[ch] = 'Y';
      }
      else if((ticker > Ctrl_ParaSK.fnkq) && (ticker <= (Ctrl_ParaSK.fnkq + Ctrl_ParaSK.fngb)))
      {
        fn_act[ch] = 0;
        Ctrl_StateFN.fnst[ch] = 'y';
      }
			else
      {
        fn_act[ch] = 0;
        Ctrl_StateFN.fnst[ch] = 'y';
      }
    }
    
    j++;
  }
}

void CtrFan_TimeCtrlISR(void)
{
	if(Ctr_TcTickers.fan_tc_ticker_en != 0)
	{
		if(Ctr_TcTickers.fan_tc_ticker++ > (Ctrl_ParaSK.fnkq + Ctrl_ParaSK.fngb))
		{
			Ctr_TcTickers.fan_tc_ticker = 0;
		}
	}
	else
	{
		Ctr_TcTickers.fan_tc_ticker = 0;
	}
}

void CtrVfd_TimeCtrlISR(void)
{
	uint8_t i;
	
	for(i=0; i<CTR_VFD_CHANNEL_ALLNUM; i++)
	{
		if(Ctr_TcTickers.vfd_tc_ticker_en[i] != 0)
		{
			if(Ctr_TcTickers.vfd_tc_ticker[i]++ > (Ctrl_ParaSK.vfkq[i] + Ctrl_ParaSK.vfgb[i]))
			{
				Ctr_TcTickers.vfd_tc_ticker[i] = 0;
			}
		}
		else
		{
			Ctr_TcTickers.vfd_tc_ticker[i] = 0;
		}
	}
}
